home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / icontext.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  9.3 KB  |  294 lines

  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: icontext.c,v 1.6 2000/09/19 19:00:42 lpd Exp $ */
  20. /* Context state operations */
  21. #include "ghost.h"
  22. #include "gsstruct.h"        /* for gxalloc.h */
  23. #include "gxalloc.h"
  24. #include "errors.h"
  25. #include "stream.h"        /* for files.h */
  26. #include "files.h"
  27. #include "idict.h"
  28. #include "igstate.h"
  29. #include "icontext.h"
  30. #include "interp.h"
  31. #include "isave.h"
  32. #include "dstack.h"
  33. #include "estack.h"
  34. #include "store.h"
  35.  
  36. /* Declare the GC descriptors for embedded objects. */
  37. extern_st(st_gs_dual_memory);
  38. extern_st(st_ref_stack);
  39. extern_st(st_dict_stack);
  40. extern_st(st_exec_stack);
  41. extern_st(st_op_stack);
  42.  
  43. /* GC descriptors */
  44. private 
  45. CLEAR_MARKS_PROC(context_state_clear_marks)
  46. {
  47.     gs_context_state_t *const pcst = vptr;
  48.  
  49.     r_clear_attrs(&pcst->stdio[0], l_mark);
  50.     r_clear_attrs(&pcst->stdio[1], l_mark);
  51.     r_clear_attrs(&pcst->stdio[2], l_mark);
  52.     r_clear_attrs(&pcst->userparams, l_mark);
  53. }
  54. private 
  55. ENUM_PTRS_WITH(context_state_enum_ptrs, gs_context_state_t *pcst) {
  56.     index -= 5;
  57.     if (index < st_gs_dual_memory_num_ptrs)
  58.     return ENUM_USING(st_gs_dual_memory, &pcst->memory,
  59.               sizeof(pcst->memory), index);
  60.     index -= st_gs_dual_memory_num_ptrs;
  61.     if (index < st_dict_stack_num_ptrs)
  62.     return ENUM_USING(st_dict_stack, &pcst->dict_stack,
  63.               sizeof(pcst->dict_stack), index);
  64.     index -= st_dict_stack_num_ptrs;
  65.     if (index < st_exec_stack_num_ptrs)
  66.     return ENUM_USING(st_exec_stack, &pcst->exec_stack,
  67.               sizeof(pcst->exec_stack), index);
  68.     index -= st_exec_stack_num_ptrs;
  69.     return ENUM_USING(st_op_stack, &pcst->op_stack,
  70.               sizeof(pcst->op_stack), index);
  71.     }
  72.     ENUM_PTR(0, gs_context_state_t, pgs);
  73.     case 1: ENUM_RETURN_REF(&pcst->stdio[0]);
  74.     case 2: ENUM_RETURN_REF(&pcst->stdio[1]);
  75.     case 3: ENUM_RETURN_REF(&pcst->stdio[2]);
  76.     case 4: ENUM_RETURN_REF(&pcst->userparams);
  77. ENUM_PTRS_END
  78. private RELOC_PTRS_WITH(context_state_reloc_ptrs, gs_context_state_t *pcst);
  79.     RELOC_PTR(gs_context_state_t, pgs);
  80.     RELOC_USING(st_gs_dual_memory, &pcst->memory, sizeof(pcst->memory));
  81.     RELOC_REF_VAR(pcst->stdio[0]);
  82.     RELOC_REF_VAR(pcst->stdio[1]);
  83.     RELOC_REF_VAR(pcst->stdio[2]);
  84.     RELOC_REF_VAR(pcst->userparams);
  85.     r_clear_attrs(&pcst->userparams, l_mark);
  86.     RELOC_USING(st_dict_stack, &pcst->dict_stack, sizeof(pcst->dict_stack));
  87.     RELOC_USING(st_exec_stack, &pcst->exec_stack, sizeof(pcst->exec_stack));
  88.     RELOC_USING(st_op_stack, &pcst->op_stack, sizeof(pcst->op_stack));
  89. RELOC_PTRS_END
  90. public_st_context_state();
  91.  
  92. /* Allocate the state of a context. */
  93. int
  94. context_state_alloc(gs_context_state_t ** ppcst,
  95.             const ref *psystem_dict,
  96.             const gs_dual_memory_t * dmem)
  97. {
  98.     gs_ref_memory_t *mem = dmem->space_local;
  99.     gs_context_state_t *pcst = *ppcst;
  100.     int code;
  101.     int i;
  102.  
  103.     if (pcst == 0) {
  104.     pcst = gs_alloc_struct((gs_memory_t *) mem, gs_context_state_t,
  105.                    &st_context_state, "context_state_alloc");
  106.     if (pcst == 0)
  107.         return_error(e_VMerror);
  108.     }
  109.     code = gs_interp_alloc_stacks(mem, pcst);
  110.     if (code < 0)
  111.     goto x0;
  112.     /*
  113.      * We have to initialize the dictionary stack early,
  114.      * for far-off references to systemdict.
  115.      */
  116.     pcst->dict_stack.system_dict = *psystem_dict;
  117.     pcst->dict_stack.min_size = 0;
  118.     pcst->pgs = int_gstate_alloc(dmem);
  119.     if (pcst->pgs == 0) {
  120.     code = gs_note_error(e_VMerror);
  121.     goto x1;
  122.     }
  123.     pcst->memory = *dmem;
  124.     pcst->language_level = 1;
  125.     make_false(&pcst->array_packing);
  126.     make_int(&pcst->binary_object_format, 0);
  127.     pcst->rand_state = rand_state_initial;
  128.     pcst->usertime_total = 0;
  129.     pcst->keep_usertime = false;
  130.     pcst->in_superexec = 0;
  131.     {    /*
  132.      * Create an empty userparams dictionary of the right size.
  133.      * If we can't determine the size, pick an arbitrary one.
  134.      */
  135.     ref *puserparams;
  136.     uint size;
  137.     ref *system_dict = &pcst->dict_stack.system_dict;
  138.  
  139.     if (dict_find_string(system_dict, "userparams", &puserparams) >= 0)
  140.         size = dict_length(puserparams);
  141.     else
  142.         size = 20;
  143.     code = dict_alloc(pcst->memory.space_local, size, &pcst->userparams);
  144.     if (code < 0)
  145.         goto x2;
  146.     /* PostScript code initializes the user parameters. */
  147.     }
  148.     pcst->scanner_options = 0;
  149.     /* The initial stdio values are bogus.... */
  150.     make_file(&pcst->stdio[0], a_readonly | avm_invalid_file_entry, 1,
  151.           invalid_file_entry);
  152.     make_file(&pcst->stdio[1], a_all | avm_invalid_file_entry, 1,
  153.           invalid_file_entry);
  154.     make_file(&pcst->stdio[2], a_all | avm_invalid_file_entry, 1,
  155.           invalid_file_entry);
  156.     for (i = countof(pcst->memory.spaces_indexed); --i >= 0;)
  157.     if (dmem->spaces_indexed[i] != 0)
  158.         ++(dmem->spaces_indexed[i]->num_contexts);
  159.     *ppcst = pcst;
  160.     return 0;
  161.   x2:gs_state_free(pcst->pgs);
  162.   x1:gs_interp_free_stacks(mem, pcst);
  163.   x0:if (*ppcst == 0)
  164.     gs_free_object((gs_memory_t *) mem, pcst, "context_state_alloc");
  165.     return code;
  166. }
  167.  
  168. /* Load the interpreter state from a context. */
  169. int
  170. context_state_load(gs_context_state_t * i_ctx_p)
  171. {
  172.     gs_ref_memory_t *lmem = iimemory_local;
  173.     ref *system_dict = systemdict;
  174.     uint space = r_space(system_dict);
  175.     dict_stack_t *dstack = &idict_stack;
  176.     int code;
  177.  
  178.     /*
  179.      * Disable save checking, and space check for systemdict, while
  180.      * copying dictionaries.
  181.      */
  182.     alloc_set_not_in_save(idmemory);
  183.     r_set_space(system_dict, avm_max);
  184.     /*
  185.      * Switch references from systemdict to local objects.
  186.      * userdict.localdicts holds these objects.  We could optimize this by
  187.      * only doing it if we're changing to a different local VM relative to
  188.      * the same global VM, but the cost is low enough relative to other
  189.      * things that we don't bother.
  190.      */
  191.     {
  192.     ref_stack_t *rdstack = &dstack->stack;
  193.     const ref *puserdict =
  194.         ref_stack_index(rdstack, ref_stack_count(rdstack) - 1 -
  195.                 dstack->userdict_index);
  196.     ref *plocaldicts;
  197.  
  198.     if (dict_find_string(puserdict, "localdicts", &plocaldicts) > 0 &&
  199.         r_has_type(plocaldicts, t_dictionary)
  200.         ) {
  201.         dict_copy(plocaldicts, system_dict, dstack);
  202.     }
  203.     }
  204.     /*
  205.      * Set systemdict.userparams to the saved copy, and then
  206.      * set the actual user parameters.  Note that we must disable both
  207.      * space checking and save checking while doing this.  Also,
  208.      * we must do this after copying localdicts (if required), because
  209.      * userparams also appears in localdicts.
  210.      */
  211.     code = dict_put_string(system_dict, "userparams", &i_ctx_p->userparams,
  212.                dstack);
  213.     if (code >= 0)
  214.     code = set_user_params(i_ctx_p, &i_ctx_p->userparams);
  215.     r_set_space(system_dict, space);
  216.     if (lmem->save_level > 0)
  217.     alloc_set_in_save(idmemory);
  218.     estack_clear_cache(&iexec_stack);
  219.     dstack_set_top(&idict_stack);
  220.     return code;
  221. }
  222.  
  223. /* Store the interpreter state in a context. */
  224. int
  225. context_state_store(gs_context_state_t * pcst)
  226. {
  227.     ref_stack_cleanup(&pcst->dict_stack.stack);
  228.     ref_stack_cleanup(&pcst->exec_stack.stack);
  229.     ref_stack_cleanup(&pcst->op_stack.stack);
  230.     /*
  231.      * The user parameters in systemdict.userparams are kept
  232.      * up to date by PostScript code, but we still need to save
  233.      * systemdict.userparams to get the correct l_new flag.
  234.      */
  235.     {
  236.     ref *puserparams;
  237.     /* We need i_ctx_p for access to the d_stack. */
  238.     i_ctx_t *i_ctx_p = pcst;
  239.  
  240.     if (dict_find_string(systemdict, "userparams", &puserparams) < 0)
  241.         return_error(e_Fatal);
  242.     pcst->userparams = *puserparams;
  243.     }
  244.     return 0;
  245. }
  246.  
  247. /* Free the contents of the state of a context, always to its local VM. */
  248. /* Return a mask of which of its VMs, if any, we freed. */
  249. int
  250. context_state_free(gs_context_state_t * pcst)
  251. {
  252.     gs_ref_memory_t *mem = pcst->memory.space_local;
  253.     int freed = 0;
  254.     int i;
  255.  
  256.     /*
  257.      * If this context is the last one referencing a particular VM
  258.      * (local / global / system), free the entire VM space;
  259.      * otherwise, just free the context-related structures.
  260.      */
  261.     for (i = countof(pcst->memory.spaces_indexed); --i >= 0;) {
  262.     if (pcst->memory.spaces_indexed[i] != 0 &&
  263.         !--(pcst->memory.spaces_indexed[i]->num_contexts)
  264.         ) {
  265. /****** FREE THE ENTIRE SPACE ******/
  266.         freed |= 1 << i;
  267.     }
  268.     }
  269.     /*
  270.      * If we freed any spaces at all, we must have freed the local
  271.      * VM where the context structure and its substructures were
  272.      * allocated.
  273.      */
  274.     if (freed)
  275.     return freed;
  276.     {
  277.     gs_state *pgs = pcst->pgs;
  278.  
  279.     gs_grestoreall(pgs);
  280.     /* Patch the saved pointer so we can do the last grestore. */
  281.     {
  282.         gs_state *saved = gs_state_saved(pgs);
  283.  
  284.         gs_state_swap_saved(saved, saved);
  285.     }
  286.     gs_grestore(pgs);
  287.     gs_state_swap_saved(pgs, (gs_state *) 0);
  288.     gs_state_free(pgs);
  289.     }
  290. /****** FREE USERPARAMS ******/
  291.     gs_interp_free_stacks(mem, pcst);
  292.     return 0;
  293. }
  294.